package ipWhite

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/admin-api/pkg/code"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/ipwhite"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/rpc"
	"github.com/zeromicro/go-zero/core/logc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateIpWhiteLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 修改
func NewUpdateIpWhiteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateIpWhiteLogic {
	return &UpdateIpWhiteLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateIpWhiteLogic) UpdateIpWhite(req *types.UpdateIpWhiteReq) (resp *types.Empty, err error) {
	if req.Id == 0 {
		return nil, code.ParamsInvalid
	}

	u, err := l.svcCtx.SettingService.IpWhite.UpdateIpWhite(l.ctx, &ipwhite.UpdateIpWhiteReq{Id: req.Id, Data: &ipwhite.AddIpWhiteReq{
		MerchantId: 1,                               // 商户ID
		Ip:         req.Data.Ip,                     // ip
		Status:     rpc.StatusType(req.Data.Status), // 是否开启,0:,关闭1:开启
		Remark:     req.Data.Remark,                 // 备注
	}})

	if err != nil {
		logc.Error(l.ctx, "UpdateIpWhite err:", err)
		return nil, err
	}

	if u.Code != int64(code.SettingRpcSuccessful.Code()) {
		return nil, code.BusinessFailure
	}
	resp = &types.Empty{}
	return
}
